package com.example.mysql.util;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;

public class ExecutorUtils
{
    public static void executor(List<Runnable> runnableList, Executor executor)
    {
        CountDownLatch ready = new CountDownLatch(runnableList.size());
        CountDownLatch start = new CountDownLatch(1);
        CountDownLatch done = new CountDownLatch(runnableList.size());
        for (Runnable runnable : runnableList)
        {
            executor.execute(() ->
            {
                ready.countDown();
                try
                {
                    start.await();
                    runnable.run();
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }finally
                {
                    done.countDown();
                }
            });
        }
        try
        {
            ready.await();
            start.countDown();
            done.await();
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
